import { createRouter, createWebHistory } from "vue-router";
import Login from "../views/Login.vue";
import Register from "../views/Register.vue";
import MeCreate from "../views/MeCreate.vue";
import Recycle from "../views/Recycle.vue";
import Questionnaire from "../views/Questionnaire.vue";
import Preview from "../views/Preview.vue";
import Deliver from "../views/Deliver.vue";
import Test from "../views/Test.vue";
import SuccessCommit from "../views/SuccessCommit.vue";
import Failure from "../views/Failure.vue";
import Setup from "../views/Setup";
import Statistics from "../views/Statistics";
import FirstShow from "../views/FirstShow.vue";
import VoteResult from "../views/VoteResult.vue";
import ExamResult from "../views/ExamResult.vue";
import axios from "axios";


const routes = [
  {
    path: "/",
    name: "FirstShow",
    component: FirstShow,
    meta: {
      keepAlive: false,
    },
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
  {
    path: "/login",
    component: Login,
    meta: {
      keepAlive: false,
    },
  },
  {
    path: "/register",
    component: Register,
    meta: {
      keepAlive: false,
    },
  },
  {
    path: "/meCreate",
    component: MeCreate,
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/recycle",
    component: Recycle,
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/setup",
    component: Setup,
  },
  {
    path: "/statistics",
    component: Statistics,
  },
  {
    path: "/questionnaire/:id",
    component: Questionnaire,
    // children: [
    //   {
    //     path: "/questionnaire/edit/:id",
    //     component: Edit,
    //   },
    // ],
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/preview/:id",
    component: Preview,
  },
  {
    path: "/deliver/:id",
    component: Deliver,
  },
  {
    path: "/test",
    component: Test,
  },
  {
    path: "/successcommit/:id",
    component: SuccessCommit,
  },
  {
    path: "/failure",
    component: Failure,
  },
  {
    path: "/voteresult/:id",
    component: VoteResult,
  },
  {
    path: "/examresult",
    component: ExamResult,
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

//路由守卫部分
router.beforeEach((to, from, next) => {
  if (
    to.path === "/meCreate" ||
    to.path === "/recycle" ||
    to.path === "/setup" ||
    to.path === "/statistics"
  ) {
    const tokenstr = localStorage.getItem("token");
    if (!tokenstr) {
      next("/login");
    }
    next();
  } else if (
    to.matched[0].path === "/questionnaire/:id" ||
    to.matched[0].path === "/preview/:id"
  ) {
    const formData = {
      questionnaire_id: to.params.id,
      username: localStorage.getItem('username')
    }
    console.log('yes')
    console.log(formData)
    axios.post("checkbeforeedit/", formData).then(response => {
      if(response.data.code == -2) {
        next(from)
      }
      else {
        next()
      }
    })
  } else {
    next();
  }
});

export default router;
